********************************************************************************
**  TITLE:      us2020_uas_contact_networks                                   **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       Various datasets					                          **
**  DATE:	    October 2022 						                          **	
**  VERSION:    Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2020_uas_contact_networks", replace

* Open survey 306

use "uas306.dta", clear

rename * *_306

rename uasid_306 uasid

save "uas306_2.dta", replace

* Open survey 308

use "uas308.dta", clear

rename * *_308

rename uasid_308 uasid

save "uas308_2.dta", replace

* Open survey 313

use "uas313.dta", clear

rename * *_313

rename uasid_313 uasid

save "uas313_2.dta", replace

* Open survey 315

use "uas315.dta", clear

rename * *_315

rename uasid_315 uasid

save "uas315_2.dta", replace

* Open survey 318

use "uas318.dta", clear

rename * *_318

rename uasid_318 uasid

save "uas318_2.dta", replace

* Open survey 306_2

use "uas306_2.dta", clear

* Merge with survey 308_2

merge 1:1 uasid using "uas308_2.dta", nogenerate force

* Merge with survey 313_2

merge 1:1 uasid using "uas313_2.dta", nogenerate force

* Merge with survey 315_2

merge 1:1 uasid using "uas315_2.dta", nogenerate force

* Merge with survey 318_2

merge 1:1 uasid using "uas318_2.dta", nogenerate force

* Series for own forecast

gen survey_s = .

replace survey_s = 308 if sc_12_trump_chance_308!=. & sc_12_biden_chance_308!=. & sc_12_other_chance_308!=. & sc_12_trump_chance_313 ==. & sc_12_biden_chance_313 ==. & sc_12_other_chance_313 ==. & sc_12_trump_chance_315 ==. & sc_12_biden_chance_315 ==. & sc_12_other_chance_315 ==.
replace survey_s = 313 if sc_12_trump_chance_313!=. & sc_12_biden_chance_313!=. & sc_12_other_chance_313!=. & sc_12_trump_chance_315 ==. & sc_12_biden_chance_315 ==. & sc_12_other_chance_315 ==.
replace survey_s = 315 if sc_12_trump_chance_315!=. & sc_12_biden_chance_315!=. & sc_12_other_chance_315!=.

gen survey_n = .

replace survey_n = 308 if sc_15_trump_chance_308!=. & sc_15_biden_chance_308!=. & sc_15_other_chance_308!=. & sc_15_trump_chance_313 ==. & sc_15_biden_chance_313 ==. & sc_15_other_chance_313 ==. & sc_15_trump_chance_315 ==. & sc_15_biden_chance_315 ==. & sc_15_other_chance_315 ==.
replace survey_n = 313 if sc_15_trump_chance_313!=. & sc_15_biden_chance_313!=. & sc_15_other_chance_313!=. & sc_15_trump_chance_315 ==. & sc_15_biden_chance_315 ==. & sc_15_other_chance_315 ==.
replace survey_n = 315 if sc_15_trump_chance_315!=. & sc_15_biden_chance_315!=. & sc_15_other_chance_315!=.

* Recode missing values

mvencode _all, mv(.a = -9999 \ .e = -9999 \ .m = -9999 \ .n = -9999)


**************************
** STATE-LEVEL OUTCOMES **
**************************

* Note: 1 = Democrat, 2 = Republican

gen statewinner_308 = .

replace statewinner_308 = 2 if statereside_308 == 1
replace statewinner_308 = 2 if statereside_308 == 2
replace statewinner_308 = 1 if statereside_308 == 4
replace statewinner_308 = 2 if statereside_308 == 5
replace statewinner_308 = 1 if statereside_308 == 6
replace statewinner_308 = 1 if statereside_308 == 8
replace statewinner_308 = 1 if statereside_308 == 9
replace statewinner_308 = 1 if statereside_308 == 10
replace statewinner_308 = 1 if statereside_308 == 11
replace statewinner_308 = 2 if statereside_308 == 12
replace statewinner_308 = 1 if statereside_308 == 13
replace statewinner_308 = 1 if statereside_308 == 15
replace statewinner_308 = 2 if statereside_308 == 16
replace statewinner_308 = 1 if statereside_308 == 17
replace statewinner_308 = 2 if statereside_308 == 18
replace statewinner_308 = 2 if statereside_308 == 19
replace statewinner_308 = 2 if statereside_308 == 20
replace statewinner_308 = 2 if statereside_308 == 21
replace statewinner_308 = 2 if statereside_308 == 22
replace statewinner_308 = 1 if statereside_308 == 23
replace statewinner_308 = 1 if statereside_308 == 24
replace statewinner_308 = 1 if statereside_308 == 25
replace statewinner_308 = 1 if statereside_308 == 26
replace statewinner_308 = 1 if statereside_308 == 27
replace statewinner_308 = 2 if statereside_308 == 28
replace statewinner_308 = 2 if statereside_308 == 29
replace statewinner_308 = 2 if statereside_308 == 30
replace statewinner_308 = 2 if statereside_308 == 31
replace statewinner_308 = 1 if statereside_308 == 32
replace statewinner_308 = 1 if statereside_308 == 33
replace statewinner_308 = 1 if statereside_308 == 34
replace statewinner_308 = 1 if statereside_308 == 35
replace statewinner_308 = 1 if statereside_308 == 36
replace statewinner_308 = 2 if statereside_308 == 37
replace statewinner_308 = 2 if statereside_308 == 38
replace statewinner_308 = 2 if statereside_308 == 39
replace statewinner_308 = 2 if statereside_308 == 40
replace statewinner_308 = 1 if statereside_308 == 41
replace statewinner_308 = 1 if statereside_308 == 42
replace statewinner_308 = 1 if statereside_308 == 44
replace statewinner_308 = 2 if statereside_308 == 45
replace statewinner_308 = 2 if statereside_308 == 46
replace statewinner_308 = 2 if statereside_308 == 47
replace statewinner_308 = 2 if statereside_308 == 48
replace statewinner_308 = 2 if statereside_308 == 49
replace statewinner_308 = 1 if statereside_308 == 50
replace statewinner_308 = 1 if statereside_308 == 51
replace statewinner_308 = 1 if statereside_308 == 53
replace statewinner_308 = 2 if statereside_308 == 54
replace statewinner_308 = 1 if statereside_308 == 55
replace statewinner_308 = 2 if statereside_308 == 56

gen statewinner_313 = .

replace statewinner_313 = 2 if statereside_313 == 1
replace statewinner_313 = 2 if statereside_313 == 2
replace statewinner_313 = 1 if statereside_313 == 4
replace statewinner_313 = 2 if statereside_313 == 5
replace statewinner_313 = 1 if statereside_313 == 6
replace statewinner_313 = 1 if statereside_313 == 8
replace statewinner_313 = 1 if statereside_313 == 9
replace statewinner_313 = 1 if statereside_313 == 10
replace statewinner_313 = 1 if statereside_313 == 11
replace statewinner_313 = 2 if statereside_313 == 12
replace statewinner_313 = 1 if statereside_313 == 13
replace statewinner_313 = 1 if statereside_313 == 15
replace statewinner_313 = 2 if statereside_313 == 16
replace statewinner_313 = 1 if statereside_313 == 17
replace statewinner_313 = 2 if statereside_313 == 18
replace statewinner_313 = 2 if statereside_313 == 19
replace statewinner_313 = 2 if statereside_313 == 20
replace statewinner_313 = 2 if statereside_313 == 21
replace statewinner_313 = 2 if statereside_313 == 22
replace statewinner_313 = 1 if statereside_313 == 23
replace statewinner_313 = 1 if statereside_313 == 24
replace statewinner_313 = 1 if statereside_313 == 25
replace statewinner_313 = 1 if statereside_313 == 26
replace statewinner_313 = 1 if statereside_313 == 27
replace statewinner_313 = 2 if statereside_313 == 28
replace statewinner_313 = 2 if statereside_313 == 29
replace statewinner_313 = 2 if statereside_313 == 30
replace statewinner_313 = 2 if statereside_313 == 31
replace statewinner_313 = 1 if statereside_313 == 32
replace statewinner_313 = 1 if statereside_313 == 33
replace statewinner_313 = 1 if statereside_313 == 34
replace statewinner_313 = 1 if statereside_313 == 35
replace statewinner_313 = 1 if statereside_313 == 36
replace statewinner_313 = 2 if statereside_313 == 37
replace statewinner_313 = 2 if statereside_313 == 38
replace statewinner_313 = 2 if statereside_313 == 39
replace statewinner_313 = 2 if statereside_313 == 40
replace statewinner_313 = 1 if statereside_313 == 41
replace statewinner_313 = 1 if statereside_313 == 42
replace statewinner_313 = 1 if statereside_313 == 44
replace statewinner_313 = 2 if statereside_313 == 45
replace statewinner_313 = 2 if statereside_313 == 46
replace statewinner_313 = 2 if statereside_313 == 47
replace statewinner_313 = 2 if statereside_313 == 48
replace statewinner_313 = 2 if statereside_313 == 49
replace statewinner_313 = 1 if statereside_313 == 50
replace statewinner_313 = 1 if statereside_313 == 51
replace statewinner_313 = 1 if statereside_313 == 53
replace statewinner_313 = 2 if statereside_313 == 54
replace statewinner_313 = 1 if statereside_313 == 55
replace statewinner_313 = 2 if statereside_313 == 56

gen statewinner_315 = .

replace statewinner_315 = 2 if statereside_315 == 1
replace statewinner_315 = 2 if statereside_315 == 2
replace statewinner_315 = 1 if statereside_315 == 4
replace statewinner_315 = 2 if statereside_315 == 5
replace statewinner_315 = 1 if statereside_315 == 6
replace statewinner_315 = 1 if statereside_315 == 8
replace statewinner_315 = 1 if statereside_315 == 9
replace statewinner_315 = 1 if statereside_315 == 10
replace statewinner_315 = 1 if statereside_315 == 11
replace statewinner_315 = 2 if statereside_315 == 12
replace statewinner_315 = 1 if statereside_315 == 13
replace statewinner_315 = 1 if statereside_315 == 15
replace statewinner_315 = 2 if statereside_315 == 16
replace statewinner_315 = 1 if statereside_315 == 17
replace statewinner_315 = 2 if statereside_315 == 18
replace statewinner_315 = 2 if statereside_315 == 19
replace statewinner_315 = 2 if statereside_315 == 20
replace statewinner_315 = 2 if statereside_315 == 21
replace statewinner_315 = 2 if statereside_315 == 22
replace statewinner_315 = 1 if statereside_315 == 23
replace statewinner_315 = 1 if statereside_315 == 24
replace statewinner_315 = 1 if statereside_315 == 25
replace statewinner_315 = 1 if statereside_315 == 26
replace statewinner_315 = 1 if statereside_315 == 27
replace statewinner_315 = 2 if statereside_315 == 28
replace statewinner_315 = 2 if statereside_315 == 29
replace statewinner_315 = 2 if statereside_315 == 30
replace statewinner_315 = 2 if statereside_315 == 31
replace statewinner_315 = 1 if statereside_315 == 32
replace statewinner_315 = 1 if statereside_315 == 33
replace statewinner_315 = 1 if statereside_315 == 34
replace statewinner_315 = 1 if statereside_315 == 35
replace statewinner_315 = 1 if statereside_315 == 36
replace statewinner_315 = 2 if statereside_315 == 37
replace statewinner_315 = 2 if statereside_315 == 38
replace statewinner_315 = 2 if statereside_315 == 39
replace statewinner_315 = 2 if statereside_315 == 40
replace statewinner_315 = 1 if statereside_315 == 41
replace statewinner_315 = 1 if statereside_315 == 42
replace statewinner_315 = 1 if statereside_315 == 44
replace statewinner_315 = 2 if statereside_315 == 45
replace statewinner_315 = 2 if statereside_315 == 46
replace statewinner_315 = 2 if statereside_315 == 47
replace statewinner_315 = 2 if statereside_315 == 48
replace statewinner_315 = 2 if statereside_315 == 49
replace statewinner_315 = 1 if statereside_315 == 50
replace statewinner_315 = 1 if statereside_315 == 51
replace statewinner_315 = 1 if statereside_315 == 53
replace statewinner_315 = 2 if statereside_315 == 54
replace statewinner_315 = 1 if statereside_315 == 55
replace statewinner_315 = 2 if statereside_315 == 56

gen statewinner_318 = .

replace statewinner_318 = 2 if statereside_318 == 1
replace statewinner_318 = 2 if statereside_318 == 2
replace statewinner_318 = 1 if statereside_318 == 4
replace statewinner_318 = 2 if statereside_318 == 5
replace statewinner_318 = 1 if statereside_318 == 6
replace statewinner_318 = 1 if statereside_318 == 8
replace statewinner_318 = 1 if statereside_318 == 9
replace statewinner_318 = 1 if statereside_318 == 10
replace statewinner_318 = 1 if statereside_318 == 11
replace statewinner_318 = 2 if statereside_318 == 12
replace statewinner_318 = 1 if statereside_318 == 13
replace statewinner_318 = 1 if statereside_318 == 15
replace statewinner_318 = 2 if statereside_318 == 16
replace statewinner_318 = 1 if statereside_318 == 17
replace statewinner_318 = 2 if statereside_318 == 18
replace statewinner_318 = 2 if statereside_318 == 19
replace statewinner_318 = 2 if statereside_318 == 20
replace statewinner_318 = 2 if statereside_318 == 21
replace statewinner_318 = 2 if statereside_318 == 22
replace statewinner_318 = 1 if statereside_318 == 23
replace statewinner_318 = 1 if statereside_318 == 24
replace statewinner_318 = 1 if statereside_318 == 25
replace statewinner_318 = 1 if statereside_318 == 26
replace statewinner_318 = 1 if statereside_318 == 27
replace statewinner_318 = 2 if statereside_318 == 28
replace statewinner_318 = 2 if statereside_318 == 29
replace statewinner_318 = 2 if statereside_318 == 30
replace statewinner_318 = 2 if statereside_318 == 31
replace statewinner_318 = 1 if statereside_318 == 32
replace statewinner_318 = 1 if statereside_318 == 33
replace statewinner_318 = 1 if statereside_318 == 34
replace statewinner_318 = 1 if statereside_318 == 35
replace statewinner_318 = 1 if statereside_318 == 36
replace statewinner_318 = 2 if statereside_318 == 37
replace statewinner_318 = 2 if statereside_318 == 38
replace statewinner_318 = 2 if statereside_318 == 39
replace statewinner_318 = 2 if statereside_318 == 40
replace statewinner_318 = 1 if statereside_318 == 41
replace statewinner_318 = 1 if statereside_318 == 42
replace statewinner_318 = 1 if statereside_318 == 44
replace statewinner_318 = 2 if statereside_318 == 45
replace statewinner_318 = 2 if statereside_318 == 46
replace statewinner_318 = 2 if statereside_318 == 47
replace statewinner_318 = 2 if statereside_318 == 48
replace statewinner_318 = 2 if statereside_318 == 49
replace statewinner_318 = 1 if statereside_318 == 50
replace statewinner_318 = 1 if statereside_318 == 51
replace statewinner_318 = 1 if statereside_318 == 53
replace statewinner_318 = 2 if statereside_318 == 54
replace statewinner_318 = 1 if statereside_318 == 55
replace statewinner_318 = 2 if statereside_318 == 56

gen statewinner = .

replace statewinner = statewinner_308 if survey_s == 308
replace statewinner = statewinner_313 if survey_s == 311
replace statewinner = statewinner_315 if survey_s == 315
replace statewinner = statewinner_318 if survey_s == 318

gen statereside = .

replace statereside = statereside_308 if survey_s == 308
replace statereside = statereside_313 if survey_s == 311
replace statereside = statereside_315 if survey_s == 315
replace statereside = statereside_318 if survey_s == 318


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by state (absolute difference between Democratic and Republican candidates)

gen margin_308 = .

replace margin_308 = 25.46165 if statereside_308 == 1
replace margin_308 = 10.06119 if statereside_308 == 2
replace margin_308 = .3087082 if statereside_308 == 4
replace margin_308 = 27.62067 if statereside_308 == 5
replace margin_308 = 29.16322 if statereside_308 == 6
replace margin_308 = 13.40694 if statereside_308 == 8
replace margin_308 = 20.07361 if statereside_308 == 9
replace margin_308 = 18.96813 if statereside_308 == 10
replace margin_308 = 86.75237 if statereside_308 == 11
replace margin_308 = 3.358368 if statereside_308 == 12
replace margin_308 = .2355843 if statereside_308 == 13
replace margin_308 = 29.19466 if statereside_308 == 15
replace margin_308 = 30.77116 if statereside_308 == 16
replace margin_308 = 16.98819 if statereside_308 == 17
replace margin_308 = 16.05948 if statereside_308 == 18
replace margin_308 = 8.152966 if statereside_308 == 19
replace margin_308 = 14.65296 if statereside_308 == 20
replace margin_308 = 25.93505 if statereside_308 == 21
replace margin_308 = 18.60943 if statereside_308 == 22
replace margin_308 = 8.97435 if statereside_308 == 23
replace margin_308 = 33.21037 if statereside_308 == 24
replace margin_308 = 33.21483 if statereside_308 == 25
replace margin_308 = 2.783527 if statereside_308 == 26
replace margin_308 = 7.110157 if statereside_308 == 27
replace margin_308 = 16.54535 if statereside_308 == 28
replace margin_308 = 15.39087 if statereside_308 == 29
replace margin_308 = 16.3691 if statereside_308 == 30
replace margin_308 = 19.05753 if statereside_308 == 31
replace margin_308 = 2.390533 if statereside_308 == 32
replace margin_308 = 7.351566 if statereside_308 == 33
replace margin_308 = 15.93768 if statereside_308 == 34
replace margin_308 = 10.79262 if statereside_308 == 35
replace margin_308 = 22.93059 if statereside_308 == 36
replace margin_308 = 1.348122 if statereside_308 == 37
replace margin_308 = 33.35729 if statereside_308 == 38
replace margin_308 = 8.031963 if statereside_308 == 39
replace margin_308 = 33.08709 if statereside_308 == 40
replace margin_308 = 16.08607 if statereside_308 == 41
replace margin_308 = 1.164883 if statereside_308 == 42
replace margin_308 = 20.80582 if statereside_308 == 44
replace margin_308 = 11.67664 if statereside_308 == 45
replace margin_308 = 26.16414 if statereside_308 == 46
replace margin_308 = 23.20886 if statereside_308 == 47
replace margin_308 = 5.57859 if statereside_308 == 48
replace margin_308 = 20.48379 if statereside_308 == 49
replace margin_308 = 35.07472 if statereside_308 == 50
replace margin_308 = 10.11401 if statereside_308 == 51
replace margin_308 = 19.20332 if statereside_308 == 53
replace margin_308 = 38.93503 if statereside_308 == 54
replace margin_308 = .6270981 if statereside_308 == 55
replace margin_308 = 43.11192 if statereside_308 == 56

gen margin_313 = .

replace margin_313 = 25.46165 if statereside_313 == 1
replace margin_313 = 10.06119 if statereside_313 == 2
replace margin_313 = .3087082 if statereside_313 == 4
replace margin_313 = 27.62067 if statereside_313 == 5
replace margin_313 = 29.16322 if statereside_313 == 6
replace margin_313 = 13.40694 if statereside_313 == 8
replace margin_313 = 20.07361 if statereside_313 == 9
replace margin_313 = 18.96813 if statereside_313 == 10
replace margin_313 = 86.75237 if statereside_313 == 11
replace margin_313 = 3.358368 if statereside_313 == 12
replace margin_313 = .2355843 if statereside_313 == 13
replace margin_313 = 29.19466 if statereside_313 == 15
replace margin_313 = 30.77116 if statereside_313 == 16
replace margin_313 = 16.98819 if statereside_313 == 17
replace margin_313 = 16.05948 if statereside_313 == 18
replace margin_313 = 8.152966 if statereside_313 == 19
replace margin_313 = 14.65296 if statereside_313 == 20
replace margin_313 = 25.93505 if statereside_313 == 21
replace margin_313 = 18.60943 if statereside_313 == 22
replace margin_313 = 8.97435 if statereside_313 == 23
replace margin_313 = 33.21037 if statereside_313 == 24
replace margin_313 = 33.21483 if statereside_313 == 25
replace margin_313 = 2.783527 if statereside_313 == 26
replace margin_313 = 7.110157 if statereside_313 == 27
replace margin_313 = 16.54535 if statereside_313 == 28
replace margin_313 = 15.39087 if statereside_313 == 29
replace margin_313 = 16.3691 if statereside_313 == 30
replace margin_313 = 19.05753 if statereside_313 == 31
replace margin_313 = 2.390533 if statereside_313 == 32
replace margin_313 = 7.351566 if statereside_313 == 33
replace margin_313 = 15.93768 if statereside_313 == 34
replace margin_313 = 10.79262 if statereside_313 == 35
replace margin_313 = 22.93059 if statereside_313 == 36
replace margin_313 = 1.348122 if statereside_313 == 37
replace margin_313 = 33.35729 if statereside_313 == 38
replace margin_313 = 8.031963 if statereside_313 == 39
replace margin_313 = 33.08709 if statereside_313 == 40
replace margin_313 = 16.08607 if statereside_313 == 41
replace margin_313 = 1.164883 if statereside_313 == 42
replace margin_313 = 20.80582 if statereside_313 == 44
replace margin_313 = 11.67664 if statereside_313 == 45
replace margin_313 = 26.16414 if statereside_313 == 46
replace margin_313 = 23.20886 if statereside_313 == 47
replace margin_313 = 5.57859 if statereside_313 == 48
replace margin_313 = 20.48379 if statereside_313 == 49
replace margin_313 = 35.07472 if statereside_313 == 50
replace margin_313 = 10.11401 if statereside_313 == 51
replace margin_313 = 19.20332 if statereside_313 == 53
replace margin_313 = 38.93503 if statereside_313 == 54
replace margin_313 = .6270981 if statereside_313 == 55
replace margin_313 = 43.11192 if statereside_313 == 56

gen margin_315 = .

replace margin_315 = 25.46165 if statereside_315 == 1
replace margin_315 = 10.06119 if statereside_315 == 2
replace margin_315 = .3087082 if statereside_315 == 4
replace margin_315 = 27.62067 if statereside_315 == 5
replace margin_315 = 29.16322 if statereside_315 == 6
replace margin_315 = 13.40694 if statereside_315 == 8
replace margin_315 = 20.07361 if statereside_315 == 9
replace margin_315 = 18.96813 if statereside_315 == 10
replace margin_315 = 86.75237 if statereside_315 == 11
replace margin_315 = 3.358368 if statereside_315 == 12
replace margin_315 = .2355843 if statereside_315 == 13
replace margin_315 = 29.19466 if statereside_315 == 15
replace margin_315 = 30.77116 if statereside_315 == 16
replace margin_315 = 16.98819 if statereside_315 == 17
replace margin_315 = 16.05948 if statereside_315 == 18
replace margin_315 = 8.152966 if statereside_315 == 19
replace margin_315 = 14.65296 if statereside_315 == 20
replace margin_315 = 25.93505 if statereside_315 == 21
replace margin_315 = 18.60943 if statereside_315 == 22
replace margin_315 = 8.97435 if statereside_315 == 23
replace margin_315 = 33.21037 if statereside_315 == 24
replace margin_315 = 33.21483 if statereside_315 == 25
replace margin_315 = 2.783527 if statereside_315 == 26
replace margin_315 = 7.110157 if statereside_315 == 27
replace margin_315 = 16.54535 if statereside_315 == 28
replace margin_315 = 15.39087 if statereside_315 == 29
replace margin_315 = 16.3691 if statereside_315 == 30
replace margin_315 = 19.05753 if statereside_315 == 31
replace margin_315 = 2.390533 if statereside_315 == 32
replace margin_315 = 7.351566 if statereside_315 == 33
replace margin_315 = 15.93768 if statereside_315 == 34
replace margin_315 = 10.79262 if statereside_315 == 35
replace margin_315 = 22.93059 if statereside_315 == 36
replace margin_315 = 1.348122 if statereside_315 == 37
replace margin_315 = 33.35729 if statereside_315 == 38
replace margin_315 = 8.031963 if statereside_315 == 39
replace margin_315 = 33.08709 if statereside_315 == 40
replace margin_315 = 16.08607 if statereside_315 == 41
replace margin_315 = 1.164883 if statereside_315 == 42
replace margin_315 = 20.80582 if statereside_315 == 44
replace margin_315 = 11.67664 if statereside_315 == 45
replace margin_315 = 26.16414 if statereside_315 == 46
replace margin_315 = 23.20886 if statereside_315 == 47
replace margin_315 = 5.57859 if statereside_315 == 48
replace margin_315 = 20.48379 if statereside_315 == 49
replace margin_315 = 35.07472 if statereside_315 == 50
replace margin_315 = 10.11401 if statereside_315 == 51
replace margin_315 = 19.20332 if statereside_315 == 53
replace margin_315 = 38.93503 if statereside_315 == 54
replace margin_315 = .6270981 if statereside_315 == 55
replace margin_315 = 43.11192 if statereside_315 == 56

gen margin_318 = .

replace margin_318 = 25.46165 if statereside_318 == 1
replace margin_318 = 10.06119 if statereside_318 == 2
replace margin_318 = .3087082 if statereside_318 == 4
replace margin_318 = 27.62067 if statereside_318 == 5
replace margin_318 = 29.16322 if statereside_318 == 6
replace margin_318 = 13.40694 if statereside_318 == 8
replace margin_318 = 20.07361 if statereside_318 == 9
replace margin_318 = 18.96813 if statereside_318 == 10
replace margin_318 = 86.75237 if statereside_318 == 11
replace margin_318 = 3.358368 if statereside_318 == 12
replace margin_318 = .2355843 if statereside_318 == 13
replace margin_318 = 29.19466 if statereside_318 == 15
replace margin_318 = 30.77116 if statereside_318 == 16
replace margin_318 = 16.98819 if statereside_318 == 17
replace margin_318 = 16.05948 if statereside_318 == 18
replace margin_318 = 8.152966 if statereside_318 == 19
replace margin_318 = 14.65296 if statereside_318 == 20
replace margin_318 = 25.93505 if statereside_318 == 21
replace margin_318 = 18.60943 if statereside_318 == 22
replace margin_318 = 8.97435 if statereside_318 == 23
replace margin_318 = 33.21037 if statereside_318 == 24
replace margin_318 = 33.21483 if statereside_318 == 25
replace margin_318 = 2.783527 if statereside_318 == 26
replace margin_318 = 7.110157 if statereside_318 == 27
replace margin_318 = 16.54535 if statereside_318 == 28
replace margin_318 = 15.39087 if statereside_318 == 29
replace margin_318 = 16.3691 if statereside_318 == 30
replace margin_318 = 19.05753 if statereside_318 == 31
replace margin_318 = 2.390533 if statereside_318 == 32
replace margin_318 = 7.351566 if statereside_318 == 33
replace margin_318 = 15.93768 if statereside_318 == 34
replace margin_318 = 10.79262 if statereside_318 == 35
replace margin_318 = 22.93059 if statereside_318 == 36
replace margin_318 = 1.348122 if statereside_318 == 37
replace margin_318 = 33.35729 if statereside_318 == 38
replace margin_318 = 8.031963 if statereside_318 == 39
replace margin_318 = 33.08709 if statereside_318 == 40
replace margin_318 = 16.08607 if statereside_318 == 41
replace margin_318 = 1.164883 if statereside_318 == 42
replace margin_318 = 20.80582 if statereside_318 == 44
replace margin_318 = 11.67664 if statereside_318 == 45
replace margin_318 = 26.16414 if statereside_318 == 46
replace margin_318 = 23.20886 if statereside_318 == 47
replace margin_318 = 5.57859 if statereside_318 == 48
replace margin_318 = 20.48379 if statereside_318 == 49
replace margin_318 = 35.07472 if statereside_318 == 50
replace margin_318 = 10.11401 if statereside_318 == 51
replace margin_318 = 19.20332 if statereside_318 == 53
replace margin_318 = 38.93503 if statereside_318 == 54
replace margin_318 = .6270981 if statereside_318 == 55
replace margin_318 = 43.11192 if statereside_318 == 56

gen margin = .

replace margin = margin_308 if survey_s == 308
replace margin = margin_313 if survey_s == 311
replace margin = margin_315 if survey_s == 315
replace margin = margin_318 if survey_s == 318


***************************
** INDEPENDENT VARIABLES **
***************************

* Time (survey 308)

gen surveydate_308=end_date_308
format %tdMon_DD,_CCYY surveydate_308

gen edate = 20201103

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time_308 = electiondate - surveydate_308

* Time (survey 313)

gen surveydate_313=end_date_313
format %tdMon_DD,_CCYY surveydate_313

gen time_313 = electiondate - surveydate_313

* Time (survey 315)

gen surveydate_315=end_date_315
format %tdMon_DD,_CCYY surveydate_315

gen time_315 = electiondate - surveydate_315

* Time

gen time = .

replace time = time_308 if survey_n == 308
replace time = time_313 if survey_n == 313
replace time = time_315 if survey_n == 315

* Gender

gen male = .

replace male = gender_308 if survey_n == 308
replace male = gender_313 if survey_n == 313
replace male = gender_315 if survey_n == 315

* Age

gen age = .

replace age = age_308 if survey_n == 308
replace age = age_313 if survey_n == 313
replace age = age_315 if survey_n == 315

replace age = . if age < 17

* Education

gen education = .

replace education = education_308 if survey_n == 308
replace education = education_313 if survey_n == 313
replace education = education_315 if survey_n == 315

* Party identification status (national)

gen pidstatus_whole = en207_306

recode pidstatus_whole 1=5 2=1 4=3 5=3 6=3 7=3 -9999=.

replace pidstatus_whole = 2 if en208_306 == 2
replace pidstatus_whole = 4 if en208_306 == 1

label define pidstatus 1 "Loser PID" 2 "Weak loser PID" 3 "No PID/Other" 4 "Weak winner PID" 5 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification status (state)

gen pidstatus_state_308 = en207_306

recode pidstatus_state_308 -9999=.

recode pidstatus_state_308 1=5 2=1 4=3 5=3 6=3 7=3 if statewinner_308 == 1
recode pidstatus_state_308 2=5 4=3 5=3 6=3 7=3 if statewinner_308 == 2

replace pidstatus_state_308 = 2 if en208_306 == 2 & statewinner_308 == 1
replace pidstatus_state_308 = 4 if en208_306 == 1 & statewinner_308 == 1

replace pidstatus_state_308 = 2 if en208_306 == 1 & statewinner_308 == 2
replace pidstatus_state_308 = 4 if en208_306 == 2 & statewinner_308 == 2

label values pidstatus_state_308 pidstatus

gen pidstatus_state_313 = en207_306

recode pidstatus_state_313 -9999=.

recode pidstatus_state_313 1=5 2=1 4=3 5=3 6=3 7=3 if statewinner_313 == 1
recode pidstatus_state_313 2=5 4=3 5=3 6=3 7=3 if statewinner_313 == 2

replace pidstatus_state_313 = 2 if en208_306 == 2 & statewinner_313 == 1
replace pidstatus_state_313 = 4 if en208_306 == 1 & statewinner_313 == 1

replace pidstatus_state_313 = 2 if en208_306 == 1 & statewinner_313 == 2
replace pidstatus_state_313 = 4 if en208_306 == 2 & statewinner_313 == 2

label values pidstatus_state_313 pidstatus

gen pidstatus_state_315 = en207_306

recode pidstatus_state_315 -9999=.

recode pidstatus_state_315 1=5 2=1 4=3 5=3 6=3 7=3 if statewinner_315 == 1
recode pidstatus_state_315 2=5 4=3 5=3 6=3 7=3 if statewinner_315 == 2

replace pidstatus_state_315 = 2 if en208_306 == 2 & statewinner_315 == 1
replace pidstatus_state_315 = 4 if en208_306 == 1 & statewinner_315 == 1

replace pidstatus_state_315 = 2 if en208_306 == 1 & statewinner_315 == 2
replace pidstatus_state_315 = 4 if en208_306 == 2 & statewinner_315 == 2

label values pidstatus_state_315 pidstatus

gen pidstatus_state = .

replace pidstatus_state = pidstatus_state_308 if survey_n == 308
replace pidstatus_state = pidstatus_state_313 if survey_n == 313
replace pidstatus_state = pidstatus_state_315 if survey_n == 315

label values pidstatus_state pidstatus

* News attentiveness

gen news_308 = sc_9_308
gen news_313 = sc_9_313
gen news_315 = sc_9_315

recode news_308 -9999=.
recode news_313 -9999=.
recode news_315 -9999=.

egen news = rowmax(news_308 news_313 news_315)

* Size

gen size_308 = sc_3_308
gen size_313 = sc_3_313
gen size_315 = sc_3_315

recode size_308 -9999=.
recode size_313 -9999=.
recode size_315 -9999=.

gen size = .

replace size = size_308 if survey_n == 308
replace size = size_313 if survey_n == 313
replace size = size_315 if survey_n == 315

replace size = 100 if size > 100 & size!=.

gen size_3pts = size
replace size_3pts = 0 if size_3pts < 2
replace size_3pts = 1 if size_3pts > 1 & size_3pts < 5
replace size_3pts = 2 if size_3pts > 4 & size_3pts!=.


******************************
** STATE-LEVEL EXPECTATIONS **
******************************

* Forecasts

gen trumpchances_state_308 = sc_12_trump_chance_308
gen bidenchances_state_308 = sc_12_biden_chance_308
gen otherchances_state_308 = sc_12_other_chance_308

gen trumpchances_state_313 = sc_12_trump_chance_313
gen bidenchances_state_313 = sc_12_biden_chance_313
gen otherchances_state_313 = sc_12_other_chance_313

gen trumpchances_state_315 = sc_12_trump_chance_315
gen bidenchances_state_315 = sc_12_biden_chance_315
gen otherchances_state_315 = sc_12_other_chance_315

recode trumpchances_state_308 -9999=.
recode bidenchances_state_308 -9999=.
recode otherchances_state_308 -9999=.

recode trumpchances_state_313 -9999=.
recode bidenchances_state_313 -9999=.
recode otherchances_state_313 -9999=.

recode trumpchances_state_315 -9999=.
recode bidenchances_state_315 -9999=.
recode otherchances_state_315 -9999=.

replace otherchances_state_308 = 100 - (trumpchances_state_308 + bidenchances_state_308) if trumpchances_state_308!=. & bidenchances_state_308!=.
replace otherchances_state_313 = 100 - (trumpchances_state_313 + bidenchances_state_313) if trumpchances_state_313!=. & bidenchances_state_313!=.
replace otherchances_state_315 = 100 - (trumpchances_state_315 + bidenchances_state_315) if trumpchances_state_315!=. & bidenchances_state_315!=.

gen complete_state_308 = 1 if trumpchances_state_308!=. & bidenchances_state_308!=.
gen complete_state_313 = 1 if trumpchances_state_313!=. & bidenchances_state_313!=.
gen complete_state_315 = 1 if trumpchances_state_315!=. & bidenchances_state_315!=.

replace complete_state_308 = 1 if trumpchances_state_308 > 50 & trumpchances_state_308!=. | bidenchances_state_308 > 50 & bidenchances_state_308!=. | otherchances_state_308 > 50 & otherchances_state_308!=.
replace complete_state_313 = 1 if trumpchances_state_313 > 50 & trumpchances_state_313!=. | bidenchances_state_313 > 50 & bidenchances_state_313!=. | otherchances_state_313 > 50 & otherchances_state_313!=.
replace complete_state_315 = 1 if trumpchances_state_315 > 50 & trumpchances_state_315!=. | bidenchances_state_315 > 50 & bidenchances_state_315!=. | otherchances_state_315 > 50 & otherchances_state_315!=.

gen forecast_state_308 = 99 if trumpchances_state_308 == bidenchances_state_308 & complete_state_308 == 1 | trumpchances_state_308 == otherchances_state_308 & complete_state_308 == 1 | bidenchances_state_308 == otherchances_state_308 & complete_state_308 == 1
replace forecast_state_308 = 2 if trumpchances_state_308 > 50 & trumpchances_state_308!=. & bidenchances_state_308 == . & otherchances_state_308 == . | trumpchances_state_308 > bidenchances_state_308 & trumpchances_state_308 > otherchances_state_308 & complete_state_308 == 1
replace forecast_state_308 = 1 if bidenchances_state_308 > 50 & bidenchances_state_308!=. & trumpchances_state_308 ==. & otherchances_state_308 == . | bidenchances_state_308 > trumpchances_state_308 & bidenchances_state_308 > otherchances_state_308 & complete_state_308 == 1
replace forecast_state_308 = 3 if otherchances_state_308 > 50 & otherchances_state_308!=1 & trumpchances_state_308 == . & bidenchances_state_308 == . | otherchances_state_308 > trumpchances_state_308 & otherchances_state_308 > bidenchances_state_308 & complete_state_308 == 1
replace forecast_state_308 = . if trumpchances_state_308 == . & bidenchances_state_308 == . & otherchances_state_308 == .

gen forecast_state_313 = 99 if trumpchances_state_313 == bidenchances_state_313 & complete_state_313 == 1 | trumpchances_state_313 == otherchances_state_313 & complete_state_313 == 1 | bidenchances_state_313 == otherchances_state_313 & complete_state_313 == 1
replace forecast_state_313 = 2 if trumpchances_state_313 > 50 & trumpchances_state_313!=. & bidenchances_state_313 == . & otherchances_state_313 == . | trumpchances_state_313 > bidenchances_state_313 & trumpchances_state_313 > otherchances_state_313 & complete_state_313 == 1
replace forecast_state_313 = 1 if bidenchances_state_313 > 50 & bidenchances_state_313!=. & trumpchances_state_313 ==. & otherchances_state_313 == . | bidenchances_state_313 > trumpchances_state_313 & bidenchances_state_313 > otherchances_state_313 & complete_state_313 == 1
replace forecast_state_313 = 3 if otherchances_state_313 > 50 & otherchances_state_313!=1 & trumpchances_state_313 == . & bidenchances_state_313 == . | otherchances_state_313 > trumpchances_state_313 & otherchances_state_313 > bidenchances_state_313 & complete_state_313 == 1
replace forecast_state_313 = . if trumpchances_state_313 == . & bidenchances_state_313 == . & otherchances_state_313 == .

gen forecast_state_315 = 99 if trumpchances_state_315 == bidenchances_state_315 & complete_state_315 == 1 | trumpchances_state_315 == otherchances_state_315 & complete_state_315 == 1 | bidenchances_state_315 == otherchances_state_315 & complete_state_315 == 1
replace forecast_state_315 = 2 if trumpchances_state_315 > 50 & trumpchances_state_315!=. & bidenchances_state_315 == . & otherchances_state_315 == . | trumpchances_state_315 > bidenchances_state_315 & trumpchances_state_315 > otherchances_state_315 & complete_state_315 == 1
replace forecast_state_315 = 1 if bidenchances_state_315 > 50 & bidenchances_state_315!=. & trumpchances_state_315 ==. & otherchances_state_315 == . | bidenchances_state_315 > trumpchances_state_315 & bidenchances_state_315 > otherchances_state_315 & complete_state_315 == 1
replace forecast_state_315 = 3 if otherchances_state_315 > 50 & otherchances_state_315!=1 & trumpchances_state_315 == . & bidenchances_state_315 == . | otherchances_state_315 > trumpchances_state_315 & otherchances_state_315 > bidenchances_state_315 & complete_state_315 == 1
replace forecast_state_315 = . if trumpchances_state_315 == . & bidenchances_state_315 == . & otherchances_state_315 == .

* Identify correct forecasts

gen correct_state_d_308 = 0 if forecast_state_308!=1 & forecast_state_308!=. & statewinner == 1
replace correct_state_d_308 = 1 if forecast_state_308 == 1 & statewinner == 1

replace correct_state_d_308 = 0 if forecast_state_308!=2 & forecast_state_308!=. & statewinner == 2
replace correct_state_d_308 = 1 if forecast_state_308 == 2 & statewinner == 2

gen correct_state_d_313 = 0 if forecast_state_313!=1 & forecast_state_313!=. & statewinner == 1
replace correct_state_d_313 = 1 if forecast_state_313 == 1 & statewinner == 1

replace correct_state_d_313 = 0 if forecast_state_313!=2 & forecast_state_313!=. & statewinner == 2
replace correct_state_d_313 = 1 if forecast_state_313 == 2 & statewinner == 2
		
gen correct_state_d_315 = 0 if forecast_state_315!=2 & forecast_state_315!=. & statewinner == 2
replace correct_state_d_315 = 1 if forecast_state_315 == 2 & statewinner == 1

replace correct_state_d_315 = 0 if forecast_state_315!=2 & forecast_state_315!=. & statewinner == 2
replace correct_state_d_315 = 1 if forecast_state_315 == 2 & statewinner == 2

gen correct_state_d = correct_state_d_308 if survey_s == 308
replace correct_state_d = correct_state_d_313 if survey_s == 313
replace correct_state_d = correct_state_d_315 if survey_s == 315


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen trumpchances_whole_308 = sc_15_trump_chance_308
gen bidenchances_whole_308 = sc_15_biden_chance_308
gen otherchances_whole_308 = sc_15_other_chance_308

gen trumpchances_whole_313 = sc_15_trump_chance_313
gen bidenchances_whole_313 = sc_15_biden_chance_313
gen otherchances_whole_313 = sc_15_other_chance_313

gen trumpchances_whole_315 = sc_15_trump_chance_315
gen bidenchances_whole_315 = sc_15_biden_chance_315
gen otherchances_whole_315 = sc_15_other_chance_315

recode trumpchances_whole_308 -9999=.
recode bidenchances_whole_308 -9999=.
recode otherchances_whole_308 -9999=.

recode trumpchances_whole_313 -9999=.
recode bidenchances_whole_313 -9999=.
recode otherchances_whole_313 -9999=.

recode trumpchances_whole_315 -9999=.
recode bidenchances_whole_315 -9999=.
recode otherchances_whole_315 -9999=.

replace otherchances_whole_308 = 100 - (trumpchances_whole_308 + bidenchances_whole_308) if trumpchances_whole_308!=. & bidenchances_whole_308!=.
replace otherchances_whole_313 = 100 - (trumpchances_whole_313 + bidenchances_whole_313) if trumpchances_whole_313!=. & bidenchances_whole_313!=.
replace otherchances_whole_315 = 100 - (trumpchances_whole_315 + bidenchances_whole_315) if trumpchances_whole_315!=. & bidenchances_whole_315!=.

gen complete_whole_308 = 1 if trumpchances_whole_308!=. & bidenchances_whole_308!=.
gen complete_whole_313 = 1 if trumpchances_whole_313!=. & bidenchances_whole_313!=.
gen complete_whole_315 = 1 if trumpchances_whole_315!=. & bidenchances_whole_315!=.

replace complete_whole_308 = 1 if trumpchances_whole_308 > 50 & trumpchances_whole_308!=. | bidenchances_whole_308 > 50 & bidenchances_whole_308!=. | otherchances_whole_308 > 50 & otherchances_whole_308!=.
replace complete_whole_313 = 1 if trumpchances_whole_313 > 50 & trumpchances_whole_313!=. | bidenchances_whole_313 > 50 & bidenchances_whole_313!=. | otherchances_whole_313 > 50 & otherchances_whole_313!=.
replace complete_whole_315 = 1 if trumpchances_whole_315 > 50 & trumpchances_whole_315!=. | bidenchances_whole_315 > 50 & bidenchances_whole_315!=. | otherchances_whole_315 > 50 & otherchances_whole_315!=.

gen forecast_whole_308 = 99 if trumpchances_whole_308 == bidenchances_whole_308 & complete_whole_308 == 1 | trumpchances_whole_308 == otherchances_whole_308 & complete_whole_308 == 1 | bidenchances_whole_308 == otherchances_whole_308 & complete_whole_308 == 1
replace forecast_whole_308 = 2 if trumpchances_whole_308 > 50 & trumpchances_whole_308!=. & bidenchances_whole_308 == . & otherchances_whole_308 == . | trumpchances_whole_308 > bidenchances_whole_308 & trumpchances_whole_308 > otherchances_whole_308 & complete_whole_308 == 1
replace forecast_whole_308 = 1 if bidenchances_whole_308 > 50 & bidenchances_whole_308!=. & trumpchances_whole_308 ==. & otherchances_whole_308 == . | bidenchances_whole_308 > trumpchances_whole_308 & bidenchances_whole_308 > otherchances_whole_308 & complete_whole_308 == 1
replace forecast_whole_308 = 3 if otherchances_whole_308 > 50 & otherchances_whole_308!=1 & trumpchances_whole_308 == . & bidenchances_whole_308 == . | otherchances_whole_308 > trumpchances_whole_308 & otherchances_whole_308 > bidenchances_whole_308 & complete_whole_308 == 1
replace forecast_whole_308 = . if trumpchances_whole_308 == . & bidenchances_whole_308 == . & otherchances_whole_308 == .

gen forecast_whole_313 = 99 if trumpchances_whole_313 == bidenchances_whole_313 & complete_whole_313 == 1 | trumpchances_whole_313 == otherchances_whole_313 & complete_whole_313 == 1 | bidenchances_whole_313 == otherchances_whole_313 & complete_whole_313 == 1
replace forecast_whole_313 = 2 if trumpchances_whole_313 > 50 & trumpchances_whole_313!=. & bidenchances_whole_313 == . & otherchances_whole_313 == . | trumpchances_whole_313 > bidenchances_whole_313 & trumpchances_whole_313 > otherchances_whole_313 & complete_whole_313 == 1
replace forecast_whole_313 = 1 if bidenchances_whole_313 > 50 & bidenchances_whole_313!=. & trumpchances_whole_313 ==. & otherchances_whole_313 == . | bidenchances_whole_313 > trumpchances_whole_313 & bidenchances_whole_313 > otherchances_whole_313 & complete_whole_313 == 1
replace forecast_whole_313 = 3 if otherchances_whole_313 > 50 & otherchances_whole_313!=1 & trumpchances_whole_313 == . & bidenchances_whole_313 == . | otherchances_whole_313 > trumpchances_whole_313 & otherchances_whole_313 > bidenchances_whole_313 & complete_whole_313 == 1
replace forecast_whole_313 = . if trumpchances_whole_313 == . & bidenchances_whole_313 == . & otherchances_whole_313 == .

gen forecast_whole_315 = 99 if trumpchances_whole_315 == bidenchances_whole_315 & complete_whole_315 == 1 | trumpchances_whole_315 == otherchances_whole_315 & complete_whole_315 == 1 | bidenchances_whole_315 == otherchances_whole_315 & complete_whole_315 == 1
replace forecast_whole_315 = 2 if trumpchances_whole_315 > 50 & trumpchances_whole_315!=. & bidenchances_whole_315 == . & otherchances_whole_315 == . | trumpchances_whole_315 > bidenchances_whole_315 & trumpchances_whole_315 > otherchances_whole_315 & complete_whole_315 == 1
replace forecast_whole_315 = 1 if bidenchances_whole_315 > 50 & bidenchances_whole_315!=. & trumpchances_whole_315 ==. & otherchances_whole_315 == . | bidenchances_whole_315 > trumpchances_whole_315 & bidenchances_whole_315 > otherchances_whole_315 & complete_whole_315 == 1
replace forecast_whole_315 = 3 if otherchances_whole_315 > 50 & otherchances_whole_315!=1 & trumpchances_whole_315 == . & bidenchances_whole_315 == . | otherchances_whole_315 > trumpchances_whole_315 & otherchances_whole_315 > bidenchances_whole_315 & complete_whole_315 == 1
replace forecast_whole_315 = . if trumpchances_whole_315 == . & bidenchances_whole_315 == . & otherchances_whole_315 == .

* Identify correct forecasts

gen correct_whole_d_308 = 0 if forecast_whole_308!=1 & forecast_whole_308!=.
replace correct_whole_d_308 = 1 if forecast_whole_308 == 1

gen correct_whole_d_313 = 0 if forecast_whole_313!=1 & forecast_whole_313!=.
replace correct_whole_d_313 = 1 if forecast_whole_313 == 1

gen correct_whole_d_315 = 0 if forecast_whole_315!=1 & forecast_whole_315!=.
replace correct_whole_d_315 = 1 if forecast_whole_315 == 1

gen correct_whole_d = correct_whole_d_308 if survey_n == 308
replace correct_whole_d = correct_whole_d_313 if survey_n == 313
replace correct_whole_d = correct_whole_d_315 if survey_n == 315


**********
** SAVE **
**********

save "us2020_uas.dta", replace


******************************
** MAIN ANALYSES (NATIONAL) **
******************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d size pidstatus_whole news age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***************************
** MAIN ANALYSES (STATE) **
***************************

* Regression analysis and percentage of correct and incorrect forecasts (state)

melogit correct_state_d size pidstatus_state news age i.male education time margin || statereside: , or
estimates table, star(.05 .01 .001)

tab correct_state_d if e(sample) == 1


***********************************
** SUMMARY STATISTICS (NATIONAL) **
***********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable size "Network size"
label variable pidstatus_whole "National PID"
label variable pidstatus_state "State PID"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

quietly logistic correct_whole_d size pidstatus_whole news age i.male education time

estpost tabstat correct_whole_d size pidstatus_whole news age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2020_uas_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2020 American presidential election (UAS), national level") replace

eststo clear


********************************
** SUMMARY STATISTICS (STATE) **
********************************

label variable correct_whole_d "Presidency winner (DV)"
label variable correct_state_d "State winner (DV)"
label variable size "Network size"
label variable pidstatus_whole "National PID"
label variable pidstatus_state "State PID"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"
label variable margin "Margin"

quietly melogit correct_state_d size pidstatus_state news age i.male education time margin || statereside: , or

estpost tabstat correct_state_d size pidstatus_state news age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2020_uas_summary_state.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2020 American presidential election (UAS), state level") replace

eststo clear

log close